package com.hzsk.lisence.controller;

import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.myexception.BaseException;
import com.hzsk.lisence.entity.EmFileEntity;
import com.hzsk.lisence.entity.query.EmFileQuery;
import com.hzsk.lisence.entity.query.IDCInfoQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.service.EmFileService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description : 授权文件对外接口
 * @Date : 2024/8/29
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@CrossOrigin
@RestController
@RequestMapping("/api/lisence/file")
public class EmFileController {
    
    @Resource
    private EmFileService emFileService;

    @PostMapping("/upload")
    public IncResponce uploadFile(@RequestParam("file") MultipartFile file, 
                                  @RequestParam("type") String type,
                                  @RequestParam("prodType") String prodType,
                                  @RequestParam("userId") Integer userId,
                                  @RequestParam("description") String  description) {
        IncResponce incResponce = new IncResponce();
        try {
            emFileService.uploadFile(file,type,prodType,userId,description);
            incResponce.setData(null);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("/page")
    public IncResponce page(@RequestBody EmFileQuery emFileQuery) {
        IncResponce incResponce = new IncResponce();
        try {
            PageInfo<EmFileEntity> filePage = emFileService.getFilePage(emFileQuery);
            incResponce.setData(filePage);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("/list")
    public IncResponce list(@RequestBody EmFileQuery emFileQuery) {
        IncResponce incResponce = new IncResponce();
        try {
            List<EmFileEntity> fileList = emFileService.getFileList(emFileQuery);
            incResponce.setData(fileList);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }

    @DeleteMapping("/delete/{id}")
    public IncResponce delete(@PathVariable("id") Integer id) {
        IncResponce incResponce = new IncResponce();
        try {
            emFileService.deleteById(id);
            incResponce.setData(null);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("/getEmpowerFile")
    @ApiOperation(value = "获取授权文件")
    public IncResponce getEmpowerFile(@RequestBody IDCInfoQuery idcInfoQuery){
        IncResponce incResponce = new IncResponce();
        try {
            Map empowerFile = emFileService.getEmpowerFile(idcInfoQuery);
            incResponce.setData(empowerFile);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }
    
}
